package com.jacky.iot;

import com.jacky.iot.resources.ApiV1Resource;
import com.jacky.iot.resources.TemperatureResource;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.config.CoapConfig;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.server.resources.Resource;
import org.eclipse.californium.elements.config.Configuration;
import org.eclipse.californium.elements.config.TcpConfig;
import org.eclipse.californium.elements.config.UdpConfig;
import org.eclipse.californium.elements.tcp.netty.TcpServerConnector;
import org.eclipse.californium.elements.util.NetworkInterfacesUtil;

import java.net.InetAddress;
import java.net.InetSocketAddress;

public class ServerStartUp extends CoapServer {

    private final String TEMP_RESOURCE = "temperature";

    static {
        CoapConfig.register();
        UdpConfig.register();
        TcpConfig.register();
    }

    public ServerStartUp() {
        CoapResource apiResource = new CoapResource("api");
        apiResource.add(new ApiV1Resource("v1"));
        add(new TemperatureResource(TEMP_RESOURCE));
        add(apiResource);
    }

    public static void main(String[] args) {
        boolean udp = true;
        boolean tcp = false;

        Integer port = Configuration.getStandard().get(CoapConfig.COAP_PORT);
        if (args.length > 0) {
            tcp = args[0].equals("coap+tcp:");
            if (tcp) {
                System.out.println("支持tcp");
            }
        }
        ServerStartUp startUp = new ServerStartUp();
        startUp.addEndpoints(udp, tcp, 5684);
        Resource root = startUp.getRoot();
        Configuration config = startUp.getConfig();
        CustomerMessageDeliverer deliverer = new CustomerMessageDeliverer(root, config);
        startUp.setMessageDeliverer(deliverer);
        startUp.start();
    }

    private void addEndpoints(boolean udp, boolean tcp, int port) {
        Configuration config = Configuration.getStandard();
        for (InetAddress address : NetworkInterfacesUtil.getNetworkInterfaces()) {
            InetSocketAddress bindAddr = new InetSocketAddress(address, port);
            if (udp) {
                CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
                builder.setInetSocketAddress(bindAddr);
                builder.setConfiguration(config);
                addEndpoint(builder.build());
                System.out.println("coap udp start up at:" + bindAddr.getHostString() + ":" + port);
            }
            if (tcp) {
                TcpServerConnector connector = new TcpServerConnector(bindAddr, config);
                CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
                builder.setConnector(connector);
                builder.setConfiguration(config);
                addEndpoint(builder.build());
                System.out.println("coap udp start up at:" + bindAddr.getHostString() + ":" + port);
            }
        }
    }
}